package com.example.zhunongzonghe.exception;

import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.common.StatusCode;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLIntegrityConstraintViolationException;

@Component
@ControllerAdvice
@ResponseBody
public class GlobalExceptionIntercepter {
    //主键重复
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result<String> sqlIntegrityConstraintViolationException(){
        return new Result<>(false, "账号已被注册，请重新输入", StatusCode.ERROR, "账号已被注册");
    }
    //文件格式不正确，
    @ExceptionHandler(FileFormatException.class)
    public Result<String> FileFormatException(){
        return new Result<>(false, "格式不正确", StatusCode.ERROR, "请传入 jpg、 png、 jpeg、 gif、webp格式的图片");
    }
    //    请求方式不正确
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result<String> httpRequestMethodNotSupportedException(){
        System.out.println("请求方式异常，httpRequestMethodNotSupportedException");
        return new Result<>(false, "请求方式不正确", StatusCode.ERROR, "请求方式不正确，HttpRequestMethodNotSupportedException");
    }
    //    类转换异常
    @ExceptionHandler(ClassCastException.class)
    public Result<String> classCastException(){
        System.out.println("类转换异常");
        return new Result<>(false,"类转换异常", StatusCode.ERROR,"类转换异常");
    }
//    //全部异常，
//    @ExceptionHandler(Exception.class)
//    public Result<String> DAException(){
//        return new Result<>(false, "有异常待解决，具体异常待看控制台",StatusCode.ERROR,"有异常待解决，具体异常待看控制台");
//    }

    @ExceptionHandler(ServiceExecption.class)
    @ResponseBody
    public Result serviceExecption(ServiceExecption e){
        return new Result<>(false, e.getMsg(), e.getCode());
    }
}
